﻿--[[
   @brief    tray_manager model
   @author   rickonzhang
   @version  1.0
   @date     2015/11/12
--]]

TRAY_STATUS = 
{
    TRAY_SYSTEM_NORMAL = 0;
    TRAY_PREDOWNLOAD_ANI = 1;
    TRAY_SYSTEM_NOTIFY = 2;
    TRAY_TALK_MESSAGE = 3;
}

tray_manager_model = {
    have_unread_msg_friend_list = {};
    have_unread_msg_group_list = {};

    tray_status_update_times = {}; --保存最新状态更新的时间


    current_tray_status = TRAY_STATUS.TRAY_SYSTEM_NORMAL;

    log = function(str)
        base_utility.log("[tray_manager_model]"..tostring(str));
    end;

    get_tray_status_list =function()
        return tray_manager_model.tray_status_update_times;
    end;

    update_tray_status_time = function(status)
        for index, item in ipairs(tray_manager_model.tray_status_update_times) do
            if item["status"] ==  status then
                tray_manager_model.tray_status_update_times[index]["time"] = os.time();
                table.sort(tray_manager_model.tray_status_update_times, function(a, b)return a["time"] > b["time"]end);
                return;
            end
        end
        local current_time = os.time();
        table.insert(tray_manager_model.tray_status_update_times, {status = status, time = current_time});
        table.sort(tray_manager_model.tray_status_update_times, function(a, b) return a["time"] > b["time"] end);
    end;

    remove_tray_status = function(status)
        for index, item in ipairs(tray_manager_model.tray_status_update_times) do
            if item["status"] ==  status then
                table.remove(tray_manager_model.tray_status_update_times, index);
            end
        end
    end;

    ----------------------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------------------------
    get_unread_msg_friend_id_list = function()
        return tray_manager_model.have_unread_msg_friend_list;
    end;

    insert_have_unread_msg_friend_id = function(friend_id)
        local already_exist = false;
        if next(tray_manager_model.have_unread_msg_friend_list) then
            for index, id in pairs(tray_manager_model.have_unread_msg_friend_list) do
                if id == friend_id then
                    already_exist = true;
                    break;
                end
            end
        end
        tray_manager_model.log("[insert_have_unread_msg_friend_id] already_exist:"..tostring(already_exist));
        if already_exist == false then
            table.insert(tray_manager_model.have_unread_msg_friend_list, friend_id);
        end
    end;

    remove_have_unread_msg_friend_id = function(friend_id)
        tray_manager_model.log("[remove_have_unread_msg_friend_id]");
        if next(tray_manager_model.have_unread_msg_friend_list) then
            for pos, id in ipairs(tray_manager_model.have_unread_msg_friend_list) do
                if id == friend_id then
                    table.remove(tray_manager_model.have_unread_msg_friend_list, pos);
                    tray_manager_model.log("[remove_have_unread_msg_friend_id] pos:"..tostring(pos));
                    break;
                end
            end
        end
    end;

    remove_all_have_unread_msg_friend = function()
        tray_manager_model.have_unread_msg_friend_list = {};
    end;
    ----------------------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------------------------
    get_unread_msg_group_id_list = function()
        return tray_manager_model.have_unread_msg_group_list;
    end;

    insert_have_unread_msg_group_id = function(group_id)
        local already_exist = false;
        if next(tray_manager_model.have_unread_msg_group_list) then
            for index, id in pairs(tray_manager_model.have_unread_msg_group_list) do
                if id == group_id then
                    already_exist = true;
                    break;
                end
            end
        end
        tray_manager_model.log("[insert_have_unread_msg_group_id] already_exist:"..tostring(already_exist));
        if already_exist == false then
            table.insert(tray_manager_model.have_unread_msg_group_list, group_id);
        end
    end;

    remove_have_unread_msg_group_id = function(group_id)
        tray_manager_model.log("[remove_have_unread_msg_group_id]");
        if next(tray_manager_model.have_unread_msg_group_list) then
            for pos, id in ipairs(tray_manager_model.have_unread_msg_group_list) do
                if id == group_id then
                    table.remove(tray_manager_model.have_unread_msg_group_list, pos);
                    tray_manager_model.log("[remove_have_unread_msg_group_id] pos:"..tostring(pos));
                    break;
                end
            end
        end
    end;

    remove_all_have_unread_msg_group = function()
        tray_manager_model.have_unread_msg_group_list = {};
    end;
    ----------------------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------------------------
}